<?php

namespace App\Models;

use App\User;
use \Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    protected $table = 'tb_order';// 表名
    public $fillable = ['time', 'user_id', 'order_state', 'totalprice', 'goods_match_id', 'address_id', 'order_id'];
    public $timestamps = false;// 关闭后  create方法不会修改 created_at updated_at 的值

    public $primaryKey = 'order_id';//主键默认是id  不是的话需要配置S

    const STATUS_NORMAL = 1;// 待支付
    const STATUS_PAYED = 2;// 已支付
    const STATUS_SHIPPING = 3;// 以发货
    const STATUS_COMPLETE = 4;//已完成


    public function getOrderStateCnAttribute()
    {
        return [
                1 => '待支付',
                2 => '已支付',
                3 => '待收货',
                4 => '已完成',
            ][$this->getOriginal('order_state')] ?? '';
    }

    public function user()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }

    public function address()
    {
        return $this->hasOne(Address::class, 'id', 'address_id');
    }

    public function goodsDetails()
    {
        return $this->hasMany(Detail::class, 'order_id', 'order_id');
    }
}
